package com.richie696.catalina;


import com.richie696.connect.http11.RequestMethod;

import java.util.*;

public class Header {

    private RequestMethod method;

    private String url;

    private String queryString;

    private final ParameterMap<String, String[]> parameters = new ParameterMap<>(20);

    private String protocol;

    private String scheme;

    private String host;

    private int port;

    private Locale locale;

    private final Map<String, String> header = new HashMap<>(15);

    public RequestMethod getMethod() {
        return method;
    }

    public void setMethod(RequestMethod method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        if (url.contains("?")) {
            this.url = url.substring(1, url.indexOf('?'));
        } else {
            this.url = url.substring(1);
        }
        setQueryString(url);
    }

    private void setQueryString(String url) {
        parameters.clear();
        if (url.contains("?")) {
            this.queryString = url.split("\\?")[1];
            String[] paramStrings = this.queryString.split("&");
            setParameters(paramStrings);
        } else {
            this.queryString = "";
        }
    }

    private void setParameters(String[] paramStrings) {
        Arrays.sort(paramStrings);
        this.parameters.setLock(false);
        for (String param : paramStrings) {
            String[] paramArray = param.split("=");
            if (this.parameters.containsKey(paramArray[0])) {
                String[] srcValues = this.parameters.get(paramArray[0]);
                String[] destValues = new String[srcValues.length + 1];
                System.arraycopy(srcValues, 0, destValues, 0, srcValues.length);
                destValues[destValues.length - 1] = paramArray[1];
                this.parameters.replace(paramArray[0], destValues);
            } else {
                this.parameters.put(paramArray[0], new String[] {paramArray[1]});
            }
        }
        this.parameters.setLock(true);
    }

    public Map<String, String[]> getParameters() {
        return parameters;
    }

    public String getQueryString() {
        return queryString;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
        this.scheme = protocol.split("/")[0].toLowerCase();
    }

    public String getHeader(String name) {
        return header.get(name);
    }

    public void addHeader(String name, String value) {
        if ("Host".equalsIgnoreCase(name)) {
            this.host = value.split(":")[0];
            this.port = Integer.parseInt(value.split(":")[1]);
        } else if ("Accept-Language".equalsIgnoreCase(name)) {
            String language = value.split(",")[0];
            this.locale = new Locale(language.split("-")[0], language.split("-")[1]);
        }
        header.put(name, value);
    }

    public String getScheme() {
        return scheme;
    }

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }

    public Locale getLocale() {
        return locale;
    }
}
